<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/1
 * Time: 19:54
 */

namespace app\api\controller;
use app\common\controller\ApiBase;
use think\Request;
use think\Controller;
use baidu\AipFace;
use app\common\model\Report as ReportModel;
// 百度人脸识别
class Fans extends ApiBase{
    protected $fans_model;
    protected $app_id;
    protected $app_key;
    protected $secret_key;
    protected $client;
    protected $report_model;

    protected function _initialize()
    {
        parent::_initialize();

        /*
         * 百度人脸识别appkey
         * */
//        $this->app_id = '0000'; // 换成申请到的appid
//        $this->app_key = 'asdfg'; // 换成申请到的app_key
//        $this->secret_key = 'asdf'; // 换成申请到的secret_key

        $this->app_id = '9921051';
        $this->app_key = 'DAECujFYFNe3xv0ONZicsOMb';
        $this->secret_key = '7qAKNOpyqBpTyPTw1H0myUTuxjZPntXS';
        $this->client = new AipFace($this->app_id,$this->app_key,$this->secret_key);
        $this->report_model = new ReportModel();
    }

    /*
     * 人脸识别, post传递图片
     * */
    public function bface(){
        if(Request::instance()->isPost()){
            $facedata = Request::instance()->post();
            $image = 'https://skin.nxhanya.com/uploads/20180825/girl.jpg';
            $imageType = 'URL';
            $options = array();
//         包括age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype信息
//        逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度
            $options["face_field"] = "age";
            if(empty($facedata['image'])){
                return $this->thinkJsonCodeMsg('',404,'没有上传用户照片');
            }else{
                $image = $facedata['image'];
                $imageType = $facedata['imagetype'];
                $options = array();
                // 想要返回的信息,例如 age,gender,faceshape,expression age 年龄,beauty美丑,expression表情,faceshape脸型,gender性别,
//                glasses是否戴眼镜,landmark 左眼有眼中心等,race人种,quality人脸质量信息,facetype 真实人脸/卡通人脸 默认只返回年龄和性别
                $options["face_field"] = empty($facedata['face_field']) ? 'age,gender':$facedata['face_field'];
                $options["max_face_num"] = empty($facedata['max_face_num']) ? 1 :$facedata['max_face_num'];
                $result = $this->client->detect($image, $imageType, $options);

            }
        }else{
            return $this->thinkJsonCodeMsg('error',404,'非法调用接口');
        }
    }

    /*
     * 完整人脸识别接口调试
     * */
    public function faceall(){
        if(Request::instance()->isPost()) {
            $facedata = Request::instance()->post();
        }else{
            return $this->thinkJsonCodeMsg('error',404,'get非法调用接口');
        }
    }

    /*
     * 百度人脸识别删减版API
     * 返回的数据记录到个人信息列表里面
     * */
    public function faceonly(){
        if(Request::instance()->isPost()) {
            $facedata = Request::instance()->post();
            $image = $facedata['image'];
            $imageType = $facedata['imagetype'];
            $result = $this->client->detect($image, $imageType);
            return json_encode($result,JSON_UNESCAPED_UNICODE);
        }
    }

    /*
     * 显示用户肌肤检测的列表
     * get
     * */
    public function showskin($openid){
        // 根据用户openid直接查询, 不需要太高的安全系数
        $map['openid'] = ['=',$openid];
        $skin = $this->report_model->where($map)->select();
        return json_encode($skin,JSON_UNESCAPED_UNICODE);
    }

    /*
     * {
error_code: 0,
error_msg: "SUCCESS",
log_id: 3545994575552,
timestamp: 1535613803,   人脸识别的时间戳
cached: 0,
result: {
face_num: 1,  人脸数量
face_list: [
{
face_token: "7eb6c68f17cf6f6728e4dbd56cf4dfec",
location: {  人脸在图片中的位置
left: 389.434082,  人脸区域离左边界的距离
top: 186.3226624,  人脸区域离上边界的距离
width: 266,  人脸区域的宽度
height: 248,  人脸区域的高度
rotation: 0  人脸框相对于竖直方向的顺时针旋转角，[-180,180]
},
face_probability: 1, 人脸置信度，范围0-1
angle: {  人脸框相对于竖直方向的顺时针旋转角，[-180,180]
yaw: 10.22792721,  三维旋转之左右旋转角[-90(左), 90(右)]
pitch: 7.323186874,  三维旋转之俯仰角度[-90(上), 90(下)]
roll: -0.7290033102   平面内旋转角[-180(逆时针), 180(顺时针)]
     * */
    /*
     * baidu 人脸识别测试接口
     * */
    public function tface(){
        $image = 'https://skin.nxhanya.com/uploads/20180825/girl.jpg';
        $imageType = 'URL';
        // 调用人脸检测
        // 如果有可选参数
        $options = array();
        $options['face_fields'] = 'age,beauty,expression,faceshape,gender,glasses,landmark,race';
//        $options["face_fields"] = "age,beauty,expression,expression_probability,gender,gender_probability,glasses,glasses_probability,occlusion,type";
       $result = $this->client->detect($image, $imageType,$options);
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function optionface(){
        $image = 'https://skin.nxhanya.com/uploads/20180825/girl.jpg';
        $imageType = 'URL';
        // 调用人脸检测
        // 如果有可选参数
        $options = array();
        $options['face_fields'] = 'age,beauty,expression,faceshape,gender,glasses,landmark,race';
//        $options["face_fields"] = "age,beauty,expression,expression_probability,gender,gender_probability,glasses,glasses_probability,occlusion,type";
        $result = $this->client->detect($image, $imageType,$options);
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    /*
     * 带参数识别更多的人脸
     * */
    public function more_face(){
        $image = 'https://skin.nxhanya.com/uploads/20180825/girl.jpg';
        $imageType = 'URL';
        // 如果有可选参数
        $options = array();
//         包括age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype信息
//        逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度
        $options["face_field"] = "age";
//         最多处理人脸的数目，默认值为1，仅检测图片中面积最大的那个人脸；最大值10
        $options["max_face_num"] = 1;
        $options["face_type"] = "LIVE";

// 带参数调用人脸检测
        $result = $this->client->detect($image, $imageType, $options);
    }

}